package com.demo290_单词规律;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeSet;

public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        boolean b = solution.wordPattern("abba", "dog dog dog dog");
        System.out.println(b);
    }
    public boolean wordPattern(String pattern, String s) {
        Map<Character, String> map = new HashMap<>();
        String[] split = s.split(" ");
        TreeSet<String> set = new TreeSet<>();
        if (split.length!=pattern.length()){
            return false;
        }
        for (int i = 0; i < pattern.length(); i++) {
            String value = split[i];
            char key = pattern.charAt(i);
            set.add(value);
            if (map.get(key) == null) {
                map.put(key, value);
            } else {
                String temp = map.get(key);
                if (!value.equals(temp)) {
                    return false;
                }
            }
        }
        if (set.size()!=map.size()){
            return false;
        }

        return true;

    }
}
